package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcBedSortMapper;
import com.csun.cmny.provider.model.domain.OmcBedSort;
import com.csun.cmny.provider.model.dto.BedSortSaveDto;
import com.csun.cmny.provider.model.vo.BedSortVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcBedSortService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-03-11 13:35
 * @Description: the class omc bed sort service impl
 */
@Slf4j
@Service
public class OmcBedSortServiceImpl extends BaseService<OmcBedSort> implements OmcBedSortService {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcBedSortMapper omcBedSortMapper;

    /**
     * query bed sort by room id
     *
     * @param roomId room id
     * @return bed sort vo
     */
    @Override
    public BedSortVo queryBedSortByRoomId(Integer roomId) {

        Preconditions.checkArgument(!PubUtils.isNull(roomId),
                cmnyMessageSource.getMessage("room.id.null", null));

        OmcBedSort omcBedSort = omcBedSortMapper.selectByPrimaryKey(roomId);
        if (PublicUtil.isNotEmpty(omcBedSort)) {
            BedSortVo bedSortVo = new BedSortVo();
            BeanUtils.copyProperties(omcBedSort, bedSortVo);

            return bedSortVo;
        }

        return null;
    }

    /**
     * save bed sort
     *
     * @param bedSortSaveDto save bed sort dto
     * @param loginAuthDto   login auth dto
     * @return the int.
     */
    @Override
    public Integer saveBedSort(BedSortSaveDto bedSortSaveDto, LoginAuthDto loginAuthDto) {

        Integer result = 0;

        OmcBedSort omcBedSort = omcBedSortMapper.selectByPrimaryKey(bedSortSaveDto.getRoomId());
        if (PublicUtil.isEmpty(omcBedSort)) {
            omcBedSort = new OmcBedSort();
            BeanUtils.copyProperties(bedSortSaveDto, omcBedSort);
            omcBedSort.setUpdateInfo(loginAuthDto);

            result = omcBedSortMapper.insertSelective(omcBedSort);
        } else {
            BeanUtils.copyProperties(bedSortSaveDto, omcBedSort);
            omcBedSort.setUpdateInfo(loginAuthDto);

            result = omcBedSortMapper.updateByPrimaryKeySelective(omcBedSort);
        }

        return result;
    }
}
